/*
 * WarehouseClient.java
 *
 * $Id: WarehouseClient.java,v 1.1 2009-09-28 10:19:12 mario Exp $
 */
package org.ceteca.explica.client.warehouse;

import java.util.ArrayList;
import java.util.HashSet;

import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.explica.core.warehouse.ProductGroupOT;
import org.ceteca.explica.core.warehouse.WarehouseOT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

public class WarehouseClient extends CommonClient {
	/**
     * Message for logging the entity glade configuration file path.
     */
    public static final String EXPLICA_LOG_EDITWAREHOUSE_GLADEPATH = "The measure unit edition interface configuration file has been found at this path: %s";
    
    public WarehouseClient() {
    	super(	WarehouseOT.class,
				InterfaceWarehouse.EXPLICA_EDIT_WAREHOUSE_GLADE_PATH,
				InterfaceWarehouse.EDIT_WAREHOUSE_DIALOG_NAME);
    }
	
    @Override
	public ArrayList getEntityFields() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void fillEntityData() {
		this.logger.info("Filling the entity with the selected registry data: " + this.entityEditGuiName);
		if (this.getSelectedEntity() != null && this.entEditView != null) {
			WarehouseOT warehouse = (WarehouseOT)this.getSelectedEntity();
			// Set up the warehouse's id
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CODE_WIDGETNAME)).setText(String.format("%d", warehouse.getIdWarehouse()));
			// Set up the warehouse's description
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_DESCRIPTION_WIDGETNAME)).setText(warehouse.getDescription());
			// Set up the warehouse's phone
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_PHONE_WIDGETNAME)).setText(warehouse.getPhone());
			// Set up the warehouse's phone
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_FAX_WIDGETNAME)).setText(warehouse.getFax());
			// Set up the warehouse's phone
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CONTACT_WIDGETNAME)).setText(warehouse.getContact());
			// Set up the warehouse's notes
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_NOTES_WIDGETNAME)).setText(warehouse.getNotes());
			// Set up the warehouse's address
			if (warehouse.getAddress() != null) {
				((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_ADDRESS_WIDGETNAME)).setText(warehouse.getAddress().getInternalValue());
				((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_ADDRESS_WIDGETNAME)).setData(warehouse.getAddress());
			}
		}
	}

	@Override
	public void clearEntityData() {
		if (this.entEditView != null) {
			// Clear the warehouse's id
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CODE_WIDGETNAME)).setText("");
			// Clear the warehouse's description
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_DESCRIPTION_WIDGETNAME)).setText("");
			// Clear the warehouse's phone
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_PHONE_WIDGETNAME)).setText("");
			// Clear the warehouse's phone
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_FAX_WIDGETNAME)).setText("");
			// Clear the warehouse's phone
			((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CONTACT_WIDGETNAME)).setText("");
			// Clear the warehouse's notes
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_NOTES_WIDGETNAME)).setText("");
			// Clear the warehouse's address
			((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_ADDRESS_WIDGETNAME)).setText("");
		}
	}

	@Override
	public BaseVO getEntityData() {
		WarehouseOT warehouse = new WarehouseOT();
		
		if (this.entEditView != null) {
			// Set up the warehouse's description
			warehouse.setDescription(((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_DESCRIPTION_WIDGETNAME)).getText());
			// Set up the warehouse's phone number
			warehouse.setPhone(((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_PHONE_WIDGETNAME)).getText());
			// Set up the warehouse's fax number
			warehouse.setFax(((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_FAX_WIDGETNAME)).getText());
			// Set up the warehouse's contact
			warehouse.setContact(((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CONTACT_WIDGETNAME)).getText());
			// Set up the warehouse's notes
			warehouse.setNotes(((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_NOTES_WIDGETNAME)).getText());
			// Set up the warehouse's address
			warehouse.setAddress((AddressOT)((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_ADDRESS_WIDGETNAME)).getData());
		}
		
		if (this.getSelectedEntity() != null) {
			WarehouseOT w = (WarehouseOT)this.getSelectedEntity();
			warehouse.setVersion(w.getVersion());
			warehouse.setIdWarehouse(w.getIdWarehouse());
		}
		
		return warehouse;
	}

	@Override
	public void updateEntityData() {
		if (this.getSelectedEntity() != null && this.entEditView != null) {
			// Set up the warehouse's description
			((WarehouseOT)this.getSelectedEntity()).setDescription(((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_DESCRIPTION_WIDGETNAME)).getText());
			// Set up the warehouse's phone number
			((WarehouseOT)this.getSelectedEntity()).setPhone(((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_PHONE_WIDGETNAME)).getText());
			// Set up the warehouse's fax number
			((WarehouseOT)this.getSelectedEntity()).setPhone(((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_FAX_WIDGETNAME)).getText());
			// Set up the warehouse's contact
			((WarehouseOT)this.getSelectedEntity()).setPhone(((Text)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CONTACT_WIDGETNAME)).getText());
			// Set up the warehouse's notes
			((WarehouseOT)this.getSelectedEntity()).setNotes(((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_NOTES_WIDGETNAME)).getText());
			// Set up the warehouse's address
			((WarehouseOT)this.getSelectedEntity()).setAddress((AddressOT)((StyledText)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_ADDRESS_WIDGETNAME)).getData());
		}
		
	}

	@Override
	public void setEditionMode(boolean enabled) {
		// Get the warehouse's code widget
		Control c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CODE_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the warehouse's description widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_DESCRIPTION_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the warehouse's phone number widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_PHONE_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the warehouse's fax number widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_FAX_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the warehouse's contact widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_CONTACT_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the warehouse's notes widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_NOTES_WIDGETNAME);
		c.setEnabled(enabled);
		
		// Get the warehouse's select address widget
		c = (Control)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_SELECT_ADDRESS_BUTTON_WIDGETNAME);
		c.setEnabled(enabled);
	}
	
	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void buildSelectionSets() {
		// Initialize the set of subentity selection widgets
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		
		// Add product group selection button
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID,
				InterfaceWarehouse.EDIT_WAREHOUSE_ADDRESS_WIDGETNAME,
				this, (Button)this.getWidget(InterfaceWarehouse.EDIT_WAREHOUSE_SELECT_ADDRESS_BUTTON_WIDGETNAME)));
	}

	@Override
	public void getWidgets() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setWidgetsValues(BaseVO entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: WarehouseClient.java,v 1.1 2009-09-28 10:19:12 mario Exp $";
	}
}
